﻿
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-collapse" lay-accordion="">
                        <div class="layui-colla-item">
                            <!--搜索区域-->
                            <h2 class="layui-colla-title">筛选查询</h2>
                            <div class="layui-colla-content layui-show">
                                <div class="layui-card">
                                    <form class="layui-form" lay-filter="SearchForm" id="SearchForm" autocomplete="off">
                                        <div class="layui-form-item">
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">:</label>
                                                <div class="layui-input-block">
                                                    <input type="radio" name="RecordStatus" value="{{colValue}}" title="{{colValue}}">
                                                </div>
                                            </div>

                                            <div class="layui-inline">
                                                <label class="layui-form-label"></label>

                                                <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="Search">搜索</button>
                                                <button class="layui-btn layui-btn-primary" id="Reset">重置</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-card">
                <div class="layui-card-body">
                    <a class="layui-btn" id="add" onclick="Add(null);">新增</a>
                    <div style="margin-top: 10px;" id="AutoTablediv">
                        <!-- 加载列表 -->
                        <table class="layui-hide" style="width:100%" id="ListTable" lay-filter="ListTable"></table>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<div class="layui-card" id="EditOrAddHtml" style="display:none;height:700px;overflow:auto">
    <div class="layui-card-body" style="padding: 15px;">
        <form class="layui-form" lay-filter="AddForm" id="AddForm" autocomplete="off">
            <input type="hidden" name="ID" class="layui-input">
            <div class="layui-form-div">
                <!--弹框编辑区-->
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='ModifiedBy' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='No' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='FactoryNo' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='DepartmentNo' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' class='layui-input' name='ModifiedOn' id='ModifiedOn' placeholder='yyyy-MM-dd' lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='ImportBatch' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='_FormID' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' class='layui-input' name='CreatedOn' id='CreatedOn' placeholder='yyyy-MM-dd' lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='OrgNo' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='CreatedBy' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='OrgName' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span style='color: red; '>*</span>:</label>
                    <div class='layui-input-block'>
                        <input type='text' name='ID' class='layui-input' placeholder="请输入" lay-verify='required'>
                    </div>
                </div>

            </div>

            <div class="layui-form-item" style="text-align:center;">
                <button class="layui-btn" lay-filter="Save" id="save" lay-submit>保存</button>
                <button class="layui-btn layui-btn-primary" type="button" id="Cancel">取消</button>
            </div>
        </form>
    </div>
</div>
<script src="/Scripts/common.js?v=4"></script>
<script>
    var form
    var table
    //接收table 列数组
    var colArry = [{ "field": "ModifiedBy", "title": null, "align": "center", "hide": false }, { "field": "No", "title": null, "align": "center", "hide": false }, { "field": "FactoryNo", "title": null, "align": "center", "hide": false }, { "field": "DepartmentNo", "title": null, "align": "center", "hide": false }, { "field": "ModifiedOn", "title": null, "align": "center", "hide": false }, { "field": "RecordStatus", "title": null, "align": "center", "hide": false }, { "field": "ImportBatch", "title": null, "align": "center", "hide": false }, { "field": "_FormID", "title": null, "align": "center", "hide": false }, { "field": "CreatedOn", "title": null, "align": "center", "hide": false }, { "field": "OrgNo", "title": null, "align": "center", "hide": false }, { "field": "CreatedBy", "title": null, "align": "center", "hide": false }, { "field": "OrgName", "title": null, "align": "center", "hide": false }, { "field": "ID", "title": null, "align": "center", "hide": false }]
    //日期控件id 数组
    var dateList =
    //编辑区日期控件ids数组
    var editDateIds = ["#ModifiedOn", "#CreatedOn"]

    //获取列表地址
    var getUrl = "/SysOrg/GetList"
    //删除地址
    var delUrl = "/SysOrg/Delete?ID="

    //弹框中保存地址
    var saveUrl = "/SysOrg/SaveOrEdit"

    layui.use(['laydate', 'table', 'form'], function () {
        var laydate = layui.laydate;
        var form = layui.form,
            table = layui.table

        form.render("select");
        //执行一个laydate实例
        for (var index = 0; index < dateList.length; index++) {
            laydate.render({
                elem: dateList[index] //指定元素
            });
        }
        //重置
        $(document).on('click', '#Reset', function () {
            $('#SearchForm')[0].reset();
            form.render(null, 'SearchForm');
            return false;
        });
        //查询
        form.on('submit(Search)', function (data) {

            //执行重载
            table.reload(tableId, {
                page: {
                    curr: 1 //重新从第 1 页开始

                },
                where: data.field
            }, 'data');

            return false;

        });
        tableId = "ListTable";

        InitTable(colArry);

        function InitTable(arry) {

            var obj1 = new Object()
            obj1.title = "操作"
            obj1.align = "center"
            obj1.templet = function (d) { var html = ''; html += '<a class=\"layui-btn layui-btn-xs\" lay-event=\"edit\" style=\"margin-right: 10px; display:none;\">编辑</a>'; html += '<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\" style=\"margin-right: 10px; display:none;\"> 删除</a>'; return html; }
            arry.push(obj1)

            //列表 初始化
            table.render({
                elem: '#' + tableId,
                url: getUrl,
                method: 'post',
                headers: setTableHeader(),
                contentType: "application/json",
                dataType: "json",
                cols: [
                    arry
                ],
                page: true,
                where: {
                    dt: new Date()
                },
                done: function (res, curr, count) {
                    GetInterfaceButton(); //设置当前页面按钮权限
                }
            });
        }


        table.on('tool(' + tableId + ')', function (obj) {

            if (obj.event == "edit") {
                Add(obj.data);
            }
            else if (obj.event == "del") {
                layer.confirm("是否删除该记录？", {
                    btn: ['确认', '取消']
                }, function () {
                    ajax(delUrl + obj.data.ID, "post", "Json", {}, function (res) {
                        if (res.code == 0) {
                            obj.del();
                            layer.msg('删除成功！', {
                                icon: 1
                            });
                        }
                    });
                }, function (index) {
                    layer.close(index);
                });
            }
        });
    });
    var pageIndex;
    //新增弹框
    function Add(data) {
        layui.use(
            ["layer", "form", "jquery", "upload", "table", "laydate"],
            function () {
                var form2 = layui.form
                var laydate = layui.laydate;
                var table2 = layui.table
                pageIndex = layer.open({
                    type: 1,
                    title: "编辑",
                    content: $("#EditOrAddHtml"),
                    area: ['700px', 'auto'],

                    success: function (layero, dIndex) {
                        $(layero).children(".layui-layer-content").css("overflow", "visible").css("height", "auto");
                        //先清空
                        $('#AddForm')[0].reset();
                        form2.val("AddForm", data);
                        for (var index = 0; index < editDateIds.length; index++) {
                            laydate.render({
                                elem: editDateIds[index] //指定元素
                            });
                        }
                        if (data == null) {
                            //如果是新增，则重置form
                            document.getElementById("AddForm").reset(); //
                        }
                        form2.render(); // 更新AddTestPaperForm内的所有表单状态
                        var mask = $(".layui-layer-shade");
                        mask.appendTo(layero.parent());
                    },
                    cancel: function () {
                        $("#EditOrAddHtml").css("display", "none")
                    }

                });
                //保存
                form2.on('submit(Save)', function (data) {

                    var bodyDat = JSON.stringify(data.field)

                    //此区域发送网络请求 把数据提交到后台  刷新列表  return false 结束
                    ajax(saveUrl, "post", "json", bodyDat, function (res) {
                        if (res.code == 0) {
                            $("#EditOrAddHtml").css("display", "none")
                            table2.reload(tableId,
                                {
                                    where: {
                                        dt: new Date()
                                    }
                                });
                            layer.close(pageIndex)
                        }
                    }, "application/json");

                    return false;
                })
                //取消
                $(document).on("click", "#Cancel", function () {
                    $("#EditOrAddHtml").css("display", "none")
                    layer.close(pageIndex);
                });
            })
    }

</script>
